package com.changgou.service.goods.controller;

import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result<List<Brand>> findAllBrand(){
        List<Brand> brandList = brandService.findAllBrand();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }
    @GetMapping("/{id}")
    public Result<Brand> findAllBrand(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK, "查询成功", brand);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "新增信息成功");
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改信息成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        brandService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除品牌信息成功");
    }

    @GetMapping("/search")
    public Result search(@RequestParam Map searchMap){
        List list = brandService.findByCondition(searchMap);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page") Integer page,@PathVariable("size")Integer size){
        Page<Brand> brandPage = brandService.findPage(page, size);
        PageResult<Brand> result = new PageResult<>(brandPage.getTotal(), brandPage.getResult());
        return new Result(true,StatusCode.OK,"查询成功",result);
    }

    @GetMapping("/searchPage/{page}/{size}")
    public Result findPageByCondition(@RequestParam Map searchMap,@PathVariable("page") Integer page,@PathVariable("size")Integer size){
        Page pageByCondition = brandService.findPageByCondition(searchMap, page, size);
        PageResult pageResult = new PageResult<>(pageByCondition.getTotal(), pageByCondition.getResult());
        return new Result(true,StatusCode.OK,"分页条件查询成功",pageResult);
    }

}
